//
//  XMPayViewController.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/6/16.
//

#import "XMPayViewController.h"
#import "XMPayView.h"
#import <WXApi.h>
#import "XMNetwork+home.h"
#import "XMPayInfoModel.h"

@interface XMPayViewController () <WXApiDelegate>

@property (nonatomic, strong) XMPayInfoModel *payInfoModel;

@end

@implementation XMPayViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
    [self getPayInfo];
}

- (void)creatUI {
    XMPayView *payView = [[XMPayView alloc] initWithFrame:self.view.bounds];
    [payView setContentWithPayPrice:_priceStr];
    [self.view addSubview:payView];
    XMWeakSelf;
    payView.payBlock = ^{
        [weakSelf pay];
    };
}

- (void)getPayInfo {
    XMWeakSelf;
    [_Network getPayInfoWithOrderId:_orderId
                            success:^(id  _Nonnull responseObject) {
         weakSelf.payInfoModel = [XMPayInfoModel yy_modelWithJSON:responseObject[@"data"]];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)pay {
    if (![WXApi isWXAppInstalled]) {
        [MBProgressHUD showError:@"您没有安装微信App，无法使用微信支付"];
        return;
    }
    [WXApi registerApp:_payInfoModel.appid/*@"wxe27fd4207b87851f"*/ universalLink:@"https://help.wechat.com/zmy/"];
    PayReq *request = [[PayReq alloc] init];
    request.partnerId = _payInfoModel.partnerId;//[data safeStringForKey:@"partnerid"];//@"1605184167";
    request.prepayId = _payInfoModel.prepayId;//[data safeStringForKey:@"prepayid"];//@"wx25105339926816c4d4f2e71b68b8980000";
    request.package = _payInfoModel.packageValue;//[data safeStringForKey:@"package"];//@"Sign=WXPay";
    request.nonceStr = _payInfoModel.noncestr;//[data safeStringForKey:@"noncestr"];//@"fM9XQ1oURZgXFSOdcBvmYfnxUhxQvqCt";
    request.timeStamp = [_payInfoModel.timestamp intValue];//[data safeIntForKey:@"timestamp"];//1643079220;
    request.sign = _payInfoModel.sign;//[data safeStringForKey:@"sign"];
    [WXApi sendReq:request completion:^(BOOL success) {//跳转支付界面结果
        if (!success) {
            [MBProgressHUD showError:@"支付信息错误"];
        }
        NSLog(@"跳转结果%@", @(success));
    }];
}

#pragma mark - Delegate
//支付结果
- (void)onResp:(BaseResp *)resp {
    switch (resp.errCode) {
        case 0: {
            [MBProgressHUD showSuccess:@"购买成功"];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
            break;
        case -1: {
            [MBProgressHUD showError:@"微信支付错误，请联系客服1"];
            
        }
            break;
        case -2: {
            [MBProgressHUD showSuccess:@"取消支付"];
            
        }
            break;
        case -3: {
            [MBProgressHUD showError:@"微信支付错误，请联系客服2"];
            
        }
            break;
        case -4: {
            [MBProgressHUD showError:@"微信支付错误，请联系客服3"];
            
        }
            break;
        case -5: {
            [MBProgressHUD showSuccess:@"微信不支持此次支付"];
        }
            break;
            
        default:
            break;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
